<?
include_once("/home/adlisting.jp/web/listing/Dao/db.php");
require_once("CheckHoliday.php");

//日付
$year = date("Y");
$month = date("m"); 
$day = date("d"); 


$date_chk = mktime(0,0,0,$month,$day,$year);


if(!ktHolidayName($date_chk)){

	$date = sprintf("%04d%02d%02d",$year,$month,$day);
	$date_mysql = sprintf("%04d-%02d-%02d",$year,$month,$day);
	echo "date:$date<br/>";
	
	$url2 = "http://searchranking.yahoo.co.jp/burst_ranking/$date";
	echo "<h1>url:$url2</h1>";
	
	$fp2 = fopen($url2,'r');
	
	if($fp2){
		$line2 = $tmp_line2 = "";
		while(!feof($fp2)){
	
			//ファイルから一行読み込む
			$line2 = fgets($fp2);
			//スペースをまとめる
			$line2 = preg_replace('/\s{2,}/', "", $line2);
			$tmp_line2 .= trim($line2);
																															
		}		
	
		$line2 = $tmp_line2;
		//echo htmlspecialchars($line2);
		//echo "<!--".$line."//-->";
	
		$matches3 = array();
		if(preg_match_all("/<li id=\"rank_([0-9]+)\" class=\"pkg\">.{5,30}<a href=\"[^>]{5,400}?\">([^<]{5,100})<\/a>.{5,300}<p>([^>]{5,100})<\/p><\/div><p class=\"rankPoint\">([^<]{1,50})<span>/i",$line2, $matches3)){
			//print_r($matches3);

			for($j=0;$j<count($matches3[1]);$j++){
				$rank = (int)$matches3[1][$j];
				$name = $matches3[2][$j];
				$comment = strip_tags($matches3[3][$j]);
				$kanma = ",";
				$point = str_replace($kanma,"",$matches3[4][$j]);

				$sql = "INSERT INTO DailyKeywordRakning(date,rank,keywordName,note,point) VALUES('$date_mysql',$rank,'$name','$comment',$point)";
				$result = mysql_query($sql);
				echo $sql;
			}

		}
	}
}




function surround_dq($word){
	return "'".$word."'";
}


function format_as_row($words=array()){
	$words_dq = array_map('surround_dq',$words);
	$row = implode(",",$words_dq)."\r\n";
	return $row;
}


function csv_out($csv,$down_file){
	// CSVの出力
	header ("Content-Type: application/force-download");
	header ("Content-Disposition: attachment; filename=" . $down_file);
	header ("Content-Description: File Transfer");
	header ("Content-Length: " . strlen(join("",$csv)) );
	foreach ($csv as $mval) {
		//echo $mval;
		echo mb_convert_encoding($mval, "Shift_JIS", "EUC-JP");
		flush();
		ob_flush();
		usleep(10000); 
	}
	exit;
}

?>
